package br.com.threads;

public class Programa33 implements Runnable{
	
	public static void main(String[] args) {
		Programa33 p = new Programa33();
		new Thread(p,"Um").start();
		new Thread(p, "Dois").start();
		new Thread(p,"Tres").start();
		new Thread(p, "Quatro").start();
	}
	public void run(){
		synchronized (Programa33.class) {
			sacar(20);
		}
	}
	public static synchronized void sacar(int valor){
		String thread = Thread.currentThread().getName();
		int saldo = 100;
		for(int i = 0; i <= 5; i++){
			saldo -= valor;
			System.out.println("Saldo: " + saldo + " " + thread);
			if(saldo <= 0){
				System.out.println("Saldo insuficiente!");
				System.exit(0);//aqui ha uma saida forcada no programa		
			}
		}
	}
	
}
